1 using System;
2 using System.Collections.Generic;
3 using ThreeDPool.EventHandlers;
4 using UnityEngine;
5
6 namespace ThreeDPool.Managers
7 {
8 /// <summary>
9 /// the responsibility of this class is to register for events and dispath them
10 /// for now this is just a basic event handler,
11 /// this helps to make sure we have registered and deregistered all the events and are not dangling
12 /// </summary>
13 public static class EventManager
14 {
15 private static Dictionary<string, Action<object, IGameEvent>> _subscribers = new Dictionary<string, Action<object, IGameEvent>>();
16
17 public static void Subscribe(string eventID, Action<object, IGameEvent> callback)
18 {
19 if (_subscribers.ContainsKey(eventID))
20 _subscribers[eventID] += callback;
21 else
22 _subscribers.Add(eventID, callback);
23 }
24
25 public static void Unsubscribe(string eventID, Action<object, IGameEvent> callback)
26 {
27 if (_subscribers.ContainsKey(eventID))
28 _subscribers[eventID] -= callback;
29 }
30
31 public static void Notify(string eventID, object sender, IGameEvent gameEvent)
32 {
33 if (_subscribers.ContainsKey(eventID))
34 {
35 // let this throw an exception so that it is properly handled during the development stage
36 Action<object, IGameEvent> selectedCallback = _subscribers[eventID];
37 Debug.Assert(selectedCallback != null, "There are no subscribed events for this " + eventID);
38
39 selectedCallback(sender, gameEvent);
40 }
41 }
42 }
43 }